package com.ikingtech.platform.service.system.dept.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ikingtech.framework.sdk.department.api.DeptOrgApi;
import com.ikingtech.framework.sdk.department.api.DeptUserApi;
import com.ikingtech.framework.sdk.department.model.DeptManagerDTO;
import com.ikingtech.framework.sdk.department.model.DeptOrgDTO;
import com.ikingtech.framework.sdk.user.api.UserDeptApi;
import com.ikingtech.framework.sdk.user.model.UserDeptDTO;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.platform.service.system.dept.entity.DepartmentDO;
import com.ikingtech.platform.service.system.dept.service.repository.DeptRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@RequiredArgsConstructor
public class UserDeptService implements UserDeptApi {

    private final DeptRepository repo;

    private final DeptOrgApi deptOrgApi;

    private final DeptUserApi deptUserApi;

    @Override
    public List<UserDeptDTO> loadByIds(List<String> deptIds) {
        if (Tools.Coll.isBlank(deptIds)) {
            return new ArrayList<>();
        }
        List<DepartmentDO> entities = this.repo.listByIds(deptIds);
        if (Tools.Coll.isBlank(entities)) {
            return new ArrayList<>();
        }
        List<DeptManagerDTO> deptManagers = this.deptUserApi.loadManagers(Tools.Coll.convertList(entities, DepartmentDO::getManagerId));
        Map<String, DeptManagerDTO> deptManagerMap = Tools.Coll.convertMap(deptManagers, DeptManagerDTO::getUserId);
        List<DeptOrgDTO> deptOrganizations = this.deptOrgApi.loadByIds(Tools.Coll.convertList(entities, DepartmentDO::getOrgId));
        Map<String, DeptOrgDTO> deptOrgMap = Tools.Coll.convertMap(deptOrganizations, DeptOrgDTO::getId);
        return Tools.Coll.convertList(entities, entity -> {
            UserDeptDTO userDept = new UserDeptDTO();
            userDept.setDeptId(entity.getId());
            userDept.setDeptName(entity.getName());
            userDept.setDeptFullPath(entity.getFullPath());
            if (deptManagerMap.containsKey(entity.getManagerId())) {
                userDept.setManagerId(deptManagerMap.get(entity.getManagerId()).getUserId());
                userDept.setManagerName(deptManagerMap.get(entity.getManagerId()).getUserName());
                userDept.setManagerAvatar(deptManagerMap.get(entity.getManagerId()).getUserAvatar());
            }
            userDept.setOrgId(entity.getOrgId());
            if (deptOrgMap.containsKey(entity.getOrgId())) {
                userDept.setOrgName(deptOrgMap.get(entity.getOrgId()).getName());
            }
            return userDept;
        });
    }

    @Override
    public List<String> loadIdAllByOrgId(String orgId) {
        if (Tools.Str.isBlank(orgId)) {
            return new ArrayList<>();
        }
        return this.repo.listObjs(Wrappers.<DepartmentDO>lambdaQuery().select(DepartmentDO::getId).eq(DepartmentDO::getOrgId, orgId), String.class::cast);
    }

    @Override
    public List<String> loadIdAllByOrgIds(List<String> orgIds) {
        if (Tools.Coll.isBlank(orgIds)) {
            return new ArrayList<>();
        }
        return this.repo.listObjs(Wrappers.<DepartmentDO>lambdaQuery().select(DepartmentDO::getId).in(DepartmentDO::getOrgId, orgIds), String.class::cast);
    }
}
